//
//  QDMetroStationTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/2.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 默认的QDMetroStationHeaderView高度
let QDMetroStationHeaderHeight = 165

protocol QDMetroStationHeaderViewDelegate: class {
    
    /// 点击的回调
    func metroStationHeaderView(view: QDMetroStationHeaderView,didActionType: QDMetroStationHeaderView.StationType)
    /// 点击交换站点
    func overStartAndEndStaion(view: QDMetroStationHeaderView)
}

extension QDMetroStationHeaderViewDelegate {
    /// 点击的回调
    func metroStationHeaderView(view: QDMetroStationHeaderView,didActionType: QDMetroStationHeaderView.StationType){}
    /// 点击交换站点
    func overStartAndEndStaion(view: QDMetroStationHeaderView){}
}

/// 起始站点的headerView
final class QDMetroStationHeaderView: UIView {

    enum StationType {
        /// 起站点
        case start
        /// 结束站点
        case end
    }
    
    /// 起始站标题
    var startTitle: String? {
        get {
            return startTextField.text
        }set {
            startTextField.text = newValue
        }
    }
    
    /// 结束站
    var endTitle: String? {
        get {
            return endTextField.text
        }set {
            endTextField.text = newValue
        }
    }
    
    /// 代理对象
    weak var delegate: QDMetroStationHeaderViewDelegate?
    
    /// 显示 起始站点的标签
    private let titleLabel = UILabel()
    /// 显示 起
    private let startLabel = UILabel()
    /// 显示 终
    private let endLabel = UILabel()
    /// 起始点转换的按钮
    private let changeButton = UIButton()
    /// 模拟横线
    private let lineView = UIView()
    /// 底部的横线
    private let bottomView = UIView()
    /// 垂直虚线
    lazy private var verticalLayer: CAShapeLayer = {
        
        let layer = CAShapeLayer()
        layer.strokeColor = #colorLiteral(red: 0.862745098, green: 0.8705882353, blue: 0.862745098, alpha: 1).cgColor
        layer.fillColor = UIColor.clear.cgColor
        layer.anchorPoint = .zero
        let rect = CGRect(x: 35, y: 78, width: 0.5, height: 34)
        layer.path = UIBezierPath(rect: rect).cgPath
        layer.lineWidth = 1
        layer.strokeStart = 0.001
        layer.strokeEnd = 0.499
        layer.lineDashPattern = [NSNumber(integerLiteral: 3)]
        
        return layer
    }()
    /// 起点的文本
    let startTextField = UITextField()
    /// 终点的文本
    let endTextField = UITextField()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        buildView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func buildView() {
        
        titleLabel.text = "起始站点"
        titleLabel.textColor = 179.ritl_color
        titleLabel.font = Font.regular.font(size: 16)
        
        startLabel.text = "起"
        startLabel.textColor = .white
        startLabel.textAlignment = .center
        startLabel.backgroundColor = QDColors.orange
        startLabel.font = Font.medium.font(size: 15)
        startLabel.clipsToBounds = true
        startLabel.layer.cornerRadius = 10
        
        endLabel.text = "终"
        endLabel.textColor = .white
        endLabel.textAlignment = .center
        endLabel.backgroundColor = QDColors.orange
        endLabel.font = Font.medium.font(size: 15)
        endLabel.clipsToBounds = true
        endLabel.layer.cornerRadius = 10
        
        changeButton.adjustsImageWhenHighlighted = false
        changeButton.setImage("ticket_btn_changestation".namedImage, for: .normal)
        changeButton.addTarget(self, action: #selector(changedButtonDidTap), for: .touchUpInside)
        
        lineView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        bottomView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        
        startTextField.textColor = QDColors.black
        startTextField.backgroundColor = .white
        startTextField.text = ""
        startTextField.placeholder = "请输入起点"
        startTextField.font = Font.regular.font(size: 16)
        startTextField.tintColor = QDColors.theme
        startTextField.delegate = self
        
        endTextField.textColor = QDColors.black
        endTextField.backgroundColor = .white
        endTextField.text = ""
        endTextField.placeholder = "请输入终点"
        endTextField.font = Font.regular.font(size: 16)
        endTextField.tintColor = QDColors.theme
        endTextField.delegate = self
        
        addSubview(titleLabel)
        addSubview(startLabel)
        addSubview(endLabel)
        addSubview(startTextField)
        addSubview(endTextField)
        addSubview(changeButton)
        addSubview(lineView)
        addSubview(bottomView)
        layer.addSublayer(verticalLayer)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(15)
            make.height.equalTo(22)
        }
        
        startLabel.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.left.equalToSuperview().offset(25)
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
        }
        
        endLabel.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.left.equalToSuperview().offset(25)
            make.top.equalToSuperview().offset(116)
        }
        
        changeButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(lineView)
            make.width.height.equalTo(37).priority(.high)
            make.right.equalToSuperview().inset(30).priority(.high)
        }
        
        startTextField.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(80)
            make.centerY.equalTo(startLabel)
            make.height.equalTo(36)
            make.right.equalTo(changeButton.snp.left).offset(-20)
        }
        
        endTextField.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(80)
            make.centerY.equalTo(endLabel)
            make.height.equalTo(36)
            make.right.equalTo(changeButton.snp.left).offset(-20).priority(.high)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.height.equalTo(0.5)
            make.top.equalToSuperview().offset(95)
            make.left.equalToSuperview().offset(78)
            make.right.equalTo(startTextField.snp.right).offset(2)
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().inset(20).priority(.high)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    /// 交换起始站点点击
    @objc func changedButtonDidTap(){
        delegate?.overStartAndEndStaion(view: self)
    }
}

extension QDMetroStationHeaderView : UITextFieldDelegate {
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        delegate?.metroStationHeaderView(view: self, didActionType: textField == startTextField ? .start : .end)
        return false
    }
}
